<?php

use yii\helpers\Html;
use mdm\admin\models\Menu;
use yii\helpers\Json;
use mdm\admin\AutocompleteAsset;
use backend\components\helper\Menu as MenuHelper;
use backend\components\widget\adminLTE\ActiveForm;;

/* @var $this yii\web\View */
/* @var $model mdm\admin\models\Menu */
/* @var $form yii\widgets\ActiveForm */
AutocompleteAsset::register($this);
$opts = Json::htmlEncode([
        'menus' => Menu::getMenuSource(),
        'routes' => Menu::getSavedRoutes(),
    ]);
$this->registerJs("var _opts = $opts;");
$this->registerJs($this->render('_script.js'));

$data = $model->data?json_decode($model->data,true):[];
$data['icon'] = $data['icon']??MenuHelper::$defaultIcon;
$data['visible'] = $data['visible']??1;
?>
<style>
    .form-group.field-menu-data{
        display: none;
    }
    .ui-autocomplete{
        max-height: 300px;
        overflow-y: scroll;
    }
</style>
<?php $form = ActiveForm::begin(); ?>
<div class="box-body">
    <?= Html::activeHiddenInput($model, 'parent', ['id' => 'parent_id']); ?>

    <?= $form->field($model, 'route')->textInput(['id' => 'route']) ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 128]) ?>

    <?= $form->field($model, 'parent_name')->textInput(['id' => 'parent_name']) ?>

    <?= $form->field($model, 'order')->input('number') ?>

    <?= $form->field($model, 'data')->hiddenInput() ?>

    <div class="form-group">
        <label class="col-sm-4 control-label" for="menu-order">图标 </label>
        <div class="col-sm-3">
            <input type="text" id="data-icon" class="form-control" value="<?=$data['icon'];?>">
        </div>
        <div class="col-sm-1">
            <a target="_blank" href="https://fontawesome.dashgame.com/" class="btn btn-link">选择</a>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-4 control-label" for="menu-order">是否显示</label>
        <div class="col-sm-4">
            <div role="radiogroup">
                <label><input type="radio" name="visible" value="1" <?=$data['visible']==1?'checked':'';?> > 是</label>
                <label><input type="radio" name="visible" value="0" <?=$data['visible']==0?'checked':'';?> > 否</label>
            </div>
        </div>
    </div>
</div>
<div class="box-footer">
    <div class="pull-right">
        <?= Html::submitButton('<i class="fa fa-save"></i> 保存', ['class' => 'btn btn-success submit-menu']) ?>
    </div>
</div>
<?php ActiveForm::end(); ?>
<script>
    $(function () {
        $('.submit-menu').click(function () {
            $('#menu-data').val(JSON.stringify({
                icon:$('#data-icon').val(),
                visible:$('input[name=visible]:checked').val(),
            }));
        });
    });
</script>